package com.iedu.demo.lgq.mapper;

import com.iedu.demo.lgq.entity.TbOrder;
import com.iedu.demo.lgq.entity.TbOrderItem;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Date;

@Mapper
public interface OrderMapper {

    // 保存订单
    @Insert("INSERT INTO tb_order ( payment, payment_type, post_fee, status, create_time, update_time, user_id, buyer_message) " +
            "VALUES ( #{payment}, #{paymentType}, #{postFee}, #{status}, #{createTime}, #{updateTime}, #{userId}, #{buyerMessage})")
    @Options(useGeneratedKeys = true,keyProperty = "orderId")
    void saveOrder(TbOrder order);

    // 查询订单通过订单ID
    @Select("SELECT * FROM tb_order WHERE order_id = #{orderId}")
    TbOrder findOrderById(@Param("orderId") Integer orderId);

    // 动态查询订单
    @Select("<script>" +
            "SELECT * FROM tb_order WHERE 1=1 " +
            "<if test='orderId != null'> AND order_id LIKE CONCAT('%', #{orderId}, '%') </if>" +
            "<if test='status != null'> AND status = #{status} </if>" +
            "<if test='userId != null'> AND user_id = #{userId} </if>" +
            "ORDER BY create_time DESC " +
            "LIMIT #{page}, #{pageSize}" +
            "</script>")
    List<TbOrder> findOrders(@Param("page") int page, @Param("pageSize") int pageSize, @Param("status") Integer status, @Param("userId") Integer userId, @Param("orderId") Integer orderId);

    // 更新订单
    @Update("UPDATE tb_order SET payment = #{payment}, payment_type = #{paymentType}, post_fee = #{postFee}, status = #{status}, " +
            "update_time = #{updateTime}, payment_time = #{paymentTime}, consign_time = #{consignTime}, end_time = #{endTime}, close_time = #{closeTime}, " +
            "shipping_name = #{shippingName}, shipping_code = #{shippingCode}, buyer_message = #{buyerMessage}, buyer_nick = #{buyerNick}, buyer_rate = #{buyerRate}, " +
            "refund_status = #{refundStatus}, refund_reason = #{refundReason}, is_refund = #{isRefund} WHERE order_id = #{orderId}")
    int updateOrder(TbOrder order);

    //商家选择快递
    @Update("UPDATE tb_order SET " +
            "shipping_name = #{shippingName}, " +
            "shipping_code = #{shippingCode}, " +
            "status = #{status}, " +
            "update_time = NOW() " +  // 更新当前时间
            "WHERE order_id = #{orderId}")
    int updateOrderForDelivery(TbOrder order);

    // 更新订单状态
    @Update("UPDATE tb_order SET status = #{status},update_time = NOW() WHERE order_id = #{orderId}")
    int updateOrderStatus(@Param("orderId") Integer orderId, @Param("status") Integer status);

    //更新订单支付状态
    @Update("UPDATE tb_order SET status = #{status}, payment_time = #{paymentTime}, update_time = NOW() WHERE order_id = #{orderId}")
    int updateOrderPayment(@Param("orderId") Integer orderId, @Param("status") Integer status, @Param("paymentTime") Date paymentTime);


    // 更新退款状态
    @Update("UPDATE tb_order SET refund_status = #{refundStatus}, refund_reason = #{refundReason}, update_time = NOW() WHERE order_id = #{orderId}")
    int updateRefundStatus(@Param("orderId") Integer orderId, @Param("refundStatus") Integer refundStatus, @Param("refundReason") String refundReason);

    // 批量删除订单
    @Delete("<script>" +
            "DELETE FROM tb_order WHERE order_id IN " +
            "<foreach collection='orderIds' item='orderId' open='(' separator=',' close=')'>" +
            "#{orderId}" +
            "</foreach>" +
            "</script>")
    void deleteOrders(@Param("orderIds") List<String> orderIds);
    //更新订单
//    @Update("UPDATE tb_order SET payment = #{payment}, payment_type = #{paymentType}, post_fee = #{postFee}, status = #{status}, " +
//            "update_time = #{updateTime}, payment_time = #{paymentTime}, consign_time = #{consignTime}, end_time = #{endTime}, close_time = #{closeTime}, " +
//            "shipping_name = #{shippingName}, shipping_code = #{shippingCode}, buyer_message = #{buyerMessage}, buyer_nick = #{buyerNick}, buyer_rate = #{buyerRate}, " +
//            "refund_status = #{refundStatus}, refund_reason = #{refundReason}, is_refund = #{isRefund} WHERE order_id = #{orderId}")
//    int updateOrder(TbOrder order);
}

